﻿using System;
using System.Collections.Generic;
using Abp.Extensions;
using Abp.Runtime.Session;
using pandx.Mulan.Auditing.Dto;
using pandx.Mulan.DataImExPorting.Excel;
using pandx.Mulan.Dto;
using pandx.Mulan.Storage;
using CollectionExtensions = Castle.Core.Internal.CollectionExtensions;

namespace pandx.Mulan.Auditing.Exporting
{
    public class AuditLogListExcelExporter:NpoiExcelExporterBase,IAuditLogListExcelExporter
    {
        private readonly IAbpSession _abpSession;
        public AuditLogListExcelExporter(IAbpSession abpSession,ITempFileCacheManager tempFileCacheManager) : base(tempFileCacheManager)
        {
            _abpSession = abpSession;
        }
        
        public FileDto ExportToExcel(List<AuditLogListDto> auditLogListDtos)
        {
            return CreateExcelPackage("审计日志列表_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.CreateSheet("审计日志列表");
                    AddHeader(sheet,
                        "执行时间",
                        "用户名",
                        "服务",
                        "操作",       
                        "参数",          
                        "持续时间",
                        "客户端",
                        "IP地址",
                        "浏览器",
                        "状态"
                    );
                    AddObjects(sheet,2,auditLogListDtos,
                        _=>_.ExecutionTime,
                        _=>_.UserName,
                        _=>_.ServiceName,
                        _=>_.MethodName,
                        _=>_.Parameters,
                        _=>_.ExecutionDuration,
                        _=>_.ClientName,
                        _=>_.ClientIpAddress,
                        _=>_.BrowserInfo,
                        _=>CollectionExtensions.IsNullOrEmpty(_.Exception)?"成功":_.Exception
                        );
                    for(var i=1;i<=auditLogListDtos.Count;i++)
                    {
                        SetCellDataFormat(sheet.GetRow(i).Cells[0],"yyyy-MM-dd HH:mm:ss");
                    }
                    

                    for (var i = 0; i < 10; i++)
                    {
                        sheet.AutoSizeColumn(i);
                    }
                });
        }
    }
}